package org.myorg.v5;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.myorg.v4.RWF4;

public class DiffResource {
	
	protected static void print(PrintWriter myFile,double a[][], int n, int j) {
		for (int i=0; i<n; i++) {
			myFile.print(a[i][j]);
			myFile.print('\t');
		}
		myFile.println();
	}
	public static void main(String[] arg) {
		System.out.println("enter main");

		//		for (int n = 100; n >= 10; n = n-10) {
		//			for (int m = 10; m <= 100; m = m + 10) {
		//				Fmap fc = new Fmap(n,m);
		//				FPeople5 my_p = NSGA.nsga(fc.getM(), 48, 0.9, 1d/fc.getM(), 1000, fc.getN(),fc);
		//			}
		//		}

		String filePath = "./cloud_map/solution" + RWF4.mkMark("./cloud_map/mark", "");

		File myFilePath = new File(filePath);
		
		if (!myFilePath.exists()) {
			try {
				myFilePath.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		int ln = 20;
		
		double nv[][] = new double[ln][3]; 
		double rv[][] = new double[ln][3]; 
		double sv[][] = new double[ln][3]; 
		double mv[][] = new double[ln][3]; 
		
		//for (double r = 0.2; r <= 0.3; r = r + 0.1) {
		for (int n = 10; n <= 110; n = n + 10) {
			System.out.println("beigin: " + n);
			for (int i=0; i<ln; i++) {
				System.out.println("beigin: " + i);
				//int n = (int) Math.ceil((Math.random()*(1000 - 100) + 100));
				int m = (int) Math.round(n*0.8);

				
				Fmap fc = new Fmap(n,m);
				//NSGA II
				FPeople5 p = NSGA.nsga(fc.getM(), 48, 0.9, 1d/fc.getM(), 1000, fc.getN(),fc);
				
				//choose the best answer
				double tmp = p.getPersons()[0].goals[0] + 
				p.getPersons()[0].goals[1] + 
				p.getPersons()[0].goals[2];
				nv[i][0] = p.getPersons()[0].goals[0];
				nv[i][1] = p.getPersons()[0].goals[1];
				nv[i][2] = p.getPersons()[0].goals[2];
				
				for (int k = 0; k < p.p_num; k++) {
					
					if (p.getPersons()[k].rank == 0){
						double tmp2 = p.getPersons()[k].goals[0] + 
						p.getPersons()[k].goals[1] + 
						p.getPersons()[k].goals[2];
						if(tmp2 < tmp) {
							nv[i][0] = p.getPersons()[k].goals[0];
							nv[i][1] = p.getPersons()[k].goals[1];
							nv[i][2] = p.getPersons()[k].goals[2];
						}
					}else{
						break;
					}
						
				}
				
				//random
				FGene5 g = Random.rand(fc);
				rv[i][0] = g.goals[0];
				rv[i][1] = g.goals[1];
				rv[i][2] = g.goals[2];
				
				//static
				g = StaticAssign.sta(fc);
				sv[i][0] = g.goals[0];
				sv[i][1] = g.goals[1];
				sv[i][2] = g.goals[2];
				
				//rank match
				g = Rank.rank(fc);
				mv[i][0] = g.goals[0];
				mv[i][1] = g.goals[1];
				mv[i][2] = g.goals[2];
				
				

			}
			
			//write file
			
			
			try {
				
				FileWriter resultFile = new FileWriter(myFilePath,true);
				PrintWriter myFile = new PrintWriter(resultFile);
				
				//print cpu
				print(myFile,nv,ln,0);
				print(myFile,rv,ln,0);
				print(myFile,sv,ln,0);
				print(myFile,mv,ln,0);
				
				//print memory
				print(myFile,nv,ln,1);
				print(myFile,rv,ln,1);
				print(myFile,sv,ln,1);
				print(myFile,mv,ln,1);
				
				//print memory
				print(myFile,nv,ln,2);
				print(myFile,rv,ln,2);
				print(myFile,sv,ln,2);
				print(myFile,mv,ln,2);


				myFile.close();
				resultFile.close();
			}
			catch (Exception e) {
				//message = "创建文件操作出错";
			}
		}

		System.out.println("leave main");
	}
}
